package cn.manyufun.myf.ui.user.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import cn.manyufun.myf.R;
import cn.manyufun.myf.app.AppConstant;
import cn.manyufun.myf.bean.UserSummary;
import cn.manyufun.myf.common.base.BaseActivity;
import cn.manyufun.myf.common.commonutils.DataCleanManager;
import cn.manyufun.myf.common.commonutils.UserInfoUtils;
import cn.manyufun.myf.common.commonwidget.NormalTitleBar;

public class SettingActivity extends BaseActivity {
    @BindView(R.id.ntb)
    NormalTitleBar ntb;
    @BindView(R.id.tv_cache)
    TextView mTvCache;
    @BindView(R.id.tv_phone)
    TextView mTvPhone;
    @BindView(R.id.ll_logout)
    View mllLogout;
    @BindView(R.id.rt_user)
    View mrtUser;

    AlertDialog alertDialog;

    @OnClick(R.id.ll_clear_cache)
    public void clear_cache() {
        alertDialog = new AlertDialog.Builder(this)
                .setTitle("提醒")
                .setMessage("确定清除所有缓存吗")
                .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        DataCleanManager.cleanInternalCache(SettingActivity.this);
                        DataCleanManager.cleanExternalCache(SettingActivity.this);

                        mTvCache.setText("0MB");
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        alertDialog.dismiss();
                    }
                }).show();
    }

    @OnClick(R.id.ll_logout)
    void logout() {
        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
        builder.setMessage("是否要登出");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setPositiveButton("登出", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                UserInfoUtils.getInstance().logout();
                mRxManager.post(AppConstant.COLLECTION_ON_LOGOUT, null);
                finish();
            }
        });
        builder.create().show();

    }

    @OnClick(R.id.ll_update)
    void update() {
        ModifyActivity.startAction(this);
    }

    public static void startAction(Context context) {
        Intent intent = new Intent(context, SettingActivity.class);
        context.startActivity(intent);
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    public void initPresenter() {

    }

    public void initView() {
        ntb.setOnBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        ntb.setTitleText(getString(R.string.setting_title));
        refreshUserInfo();
        try {
            mTvCache.setText(DataCleanManager.getCacheSize(getCacheDir()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void refreshUserInfo() {
        if (UserInfoUtils.getInstance().checkLogin()) {
            UserSummary user = UserInfoUtils.getInstance().getUser();
            mllLogout.setVisibility(View.VISIBLE);
            mrtUser.setVisibility(View.VISIBLE);
            mTvPhone.setText(user.PhoneStr());
        } else {
            mllLogout.setVisibility(View.GONE);
            mrtUser.setVisibility(View.GONE);
        }

    }
}
